package com.photoveil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class TextEditActivity extends Activity {
		
	private static TextPreference textPreference;
	private static Button doneButton;
	private static Button cancelButton;
	
	private static EditText editText;
	private static Spinner sizeSelector;
	private static Spinner colourSelector;
	private static Spinner positionSelector;
	private static Spinner fontSelector;
	private static Spinner styleSelector;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_text);
		doneButton = (Button) findViewById(R.id.text_edit_done_button);
		cancelButton = (Button) findViewById(R.id.text_edit_cancel_button);
		editText = (EditText) findViewById(R.id.editTextField);
		sizeSelector = (Spinner) findViewById(R.id.size_selector);
		colourSelector = (Spinner) findViewById(R.id.colour_selector);
		positionSelector = (Spinner) findViewById(R.id.position_selector);
		fontSelector = (Spinner) findViewById(R.id.font_selector);
		styleSelector = (Spinner) findViewById(R.id.style_selector);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle != null) {
			textPreference = (TextPreference) bundle.get("text_preferences");
			editText.setText(textPreference.getText());
			Log.d("PhotoVeil", "TextPreference received and loaded. Text: " + textPreference.getText());
		} else {
			Log.d("PhotoVeil", "No TextPreference loaded. New layer.");
		}
		
		initializeButtons();
		initializeSpinners();
	}
	
	private void initializeButtons() {
		doneButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				
				
				textPreference = new TextPreference(editText.getText().toString(), 
						(String)sizeSelector.getSelectedItem(),
						(String)colourSelector.getSelectedItem(),
						positionSelector.getSelectedItemPosition(),
						fontSelector.getSelectedItemPosition(),
						styleSelector.getSelectedItemPosition());
				
				bundle.putSerializable("text_preferences", textPreference);
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				
				finish();				
			}
		});		
		
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
	
	private void initializeSpinners() {
		ArrayAdapter<CharSequence> sizeAdapter = ArrayAdapter.createFromResource(this, R.array.font_size_items, android.R.layout.simple_spinner_item);
		sizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sizeSelector.setAdapter(sizeAdapter);
		
		ArrayAdapter<CharSequence> colourAdapter = ArrayAdapter.createFromResource(this, R.array.colour_items, android.R.layout.simple_spinner_item);
		colourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		colourSelector.setAdapter(colourAdapter);
		
		ArrayAdapter<CharSequence> positionAdapter = ArrayAdapter.createFromResource(this, R.array.position_items, android.R.layout.simple_spinner_item);
		positionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		positionSelector.setAdapter(positionAdapter);
		
		ArrayAdapter<CharSequence> fontAdapter = ArrayAdapter.createFromResource(this, R.array.font_items, android.R.layout.simple_spinner_item);
		fontAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		fontSelector.setAdapter(fontAdapter);
		
		ArrayAdapter<CharSequence> styleAdapter = ArrayAdapter.createFromResource(this, R.array.style_items, android.R.layout.simple_spinner_item);
		styleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		styleSelector.setAdapter(styleAdapter);
		
		
		if (textPreference != null) {
			sizeSelector.setSelection(TextPreference.SIZE_POSITION_MAP.get(textPreference.getSize()));
			colourSelector.setSelection(TextPreference.COLOUR_POSITION_MAP.get(textPreference.getColour()));
			positionSelector.setSelection(textPreference.getPosition());
			fontSelector.setSelection(textPreference.getFontPosition());
			styleSelector.setSelection(textPreference.getStylePosition());
		} else {
			sizeSelector.setSelection(1);
			colourSelector.setSelection(0);
			positionSelector.setSelection(0);
			fontSelector.setSelection(0);
			styleSelector.setSelection(0);
		}
	}
}
